import { createStore } from "vuex";

const moduleA = {
  state() {
    return {
      count: 0,
    };
  },
  mutations: {
    incrementMA(state) {
      state.count++;
    },
  },
};

const vuex = createStore({
  modules: {
    a: moduleA,
  },
  state() {
    return {
      count: 0,
      todos: [
        { id: 1, text: "11111", done: true },
        { id: 2, text: "22222", done: false },
      ],
    };
  },
  getters: {
    doneTodos(state) {
      return state.todos.filter((todo) => todo.done);
    },
    doneTodosCount(state, getters) {
      return getters.doneTodos.length;
    },
    getTodoById: (state) => (id) => {
      return state.todos.find((todo) => todo.id === id);
    },
  },
  mutations: {
    increment(state, payload) {
      state.count += payload.amount;
    },
  },
  actions: {
    incrementAsync(context, payload) {
      setTimeout(() => {
        context.commit({
          type: "increment",
          amount: payload.amount,
        });
      }, 1000);
    },
  },
});

export default vuex;
